package com.example.news;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private MyTouchListener myTouchListener;//实现接口
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
        NavController controller = Navigation.findNavController(this,R.id.nav_host_fragment_activity_main);
        AppBarConfiguration configuration = new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build();
//        NavigationUI.setupActionBarWithNavController(this,controller,configuration);
        NavigationUI.setupWithNavController(bottomNavigationView,controller);

    }
    @SuppressLint("ResourceType")
    public void click(View view)
    {
        Bundle bundle = new Bundle();
        switch (view.getId()){
            case R.id.game:bundle.putInt("type",5);bundle.putInt("image",R.drawable.game);break;
            case R.id.sport: bundle.putInt("type",0);bundle.putInt("image",R.drawable.sport);break;
            case R.id.fashin: bundle.putInt("type",1);bundle.putInt("image",R.drawable.fashin);break;
            case R.id.headline:bundle.putInt("type",7);bundle.putInt("image",R.drawable.headline);break;
            case R.id.mainnews:bundle.putInt("type",3);bundle.putInt("image",R.drawable.mainnews);break;
            case R.id.military:bundle.putInt("type",2);bundle.putInt("image",R.drawable.military);break;
            case R.id.health:bundle.putInt("type",5);bundle.putInt("image",R.drawable.health);break;
            case R.id.stock:bundle.putInt("type",4);bundle.putInt("image",R.drawable.stock);break;
        }
        NavController controller = Navigation.findNavController(this,R.id.nav_host_fragment_activity_main);
        controller.navigate(R.id.action_homeFragment_to_listFragment,bundle);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //将触摸事件传递给回调函数
        if (null != myTouchListener) {
            myTouchListener.onTouch(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 用于注册回调事件
     */
    public void registerMyTouchListener(MyTouchListener myTouchListener) {
        this.myTouchListener = myTouchListener;
    }

    /**
     * 定义一个接口
     * @author fox
     *
     */
    public interface MyTouchListener {
        public void onTouch(MotionEvent ev);
    }

}